/**
 * 获取Code
 * @returns
 */
function getCode() {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        resolve(res.code);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

/**
 * 获取日期
 * @param {*} format
 * @returns
 */
function getDate(format = "yyyy-mm-dd") {
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();

  if (format === "yyyy-mm-dd") {
    return Promise.resolve(`${year}-${month}-${day}`);
  }
  if (format === "yyyy-mm") {
    return Promise.resolve(`${year}-${month}`);
  }
  if (format === "yyyy") {
    return Promise.resolve(`${year}`);
  }
  if (format === "mm") {
    return Promise.resolve(`${month}`);
  }
  if (format === "dd") {
    return Promise.resolve(`${day}`);
  }
  return `${year}-${month}-${day}`;
}

module.exports = {
  getCode,
  getDate,
};


// 调用
// Path: pages/index/index.js
const { getCode, getDate } = require("../../utils/util.js");

getCode().then((code) => {
    console.log(code);
});